package org.dawn.deveco.plugin.resourceeditor.window.model;


import org.dawn.deveco.plugin.resourceeditor.common.Constants;
import org.dawn.deveco.plugin.resourceeditor.model.MediaResourceContent;
import org.dawn.deveco.plugin.resourceeditor.model.interfaces.ResourceContent;
import org.dawn.deveco.plugin.resourceeditor.utils.Logger;
import org.dawn.deveco.plugin.resourceeditor.utils.SwingUtils;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.Vector;

public final class MyTableModel extends DefaultTableModel {
    /**
     * 资源类别：string, color, dimension, media
     */
    private final String category;

    /**
     * 资源内部分组。如：dimension 内的 float, int, double
     */
    private final String group;

    /**
     * 列的数据类型
     */
    private final Class[] columnClasses;

    private final ResourceContent resourceContent;
    private JTable table;

    private boolean changed = false;
    private boolean addNew = false;
    private Object oldValue;

    public static final Logger LOG = Logger.getInstance(MyTableModel.class);

    public MyTableModel(ResourceContent content, String group) {
        super();
        this.category = content.getCategory();
        this.group = group;
        this.columnClasses = content.getColumnClasses();
        this.resourceContent = content;

        Vector<Vector> data = resourceContent.getJTableContent(group);
        resourceContent.syncGroupData(group, data);

        if (Constants.MEDIA_CATEGORY.equals(this.category)) {
            loadImage(data);
        }
        this.setDataVector(data, content.getHeader());
    }

    public String getCategory() {
        return this.category;
    }

    public String getGroup() {
        return this.group;
    }

    public JTable getTable() {
        return this.table;
    }

    public void setTable(JTable table) {
        this.table = table;
    }

    public ResourceContent getResourceContent() {
        return resourceContent;
    }

    public boolean isChanged() {
        return changed;
    }

    public void setChanged(boolean changed) {
        this.changed = changed;
    }

    public boolean isAddNew() {
        return addNew;
    }

    public void setAddNew(boolean addNew) {
        this.addNew = addNew;
    }

    public Object getOldValue() {
        return oldValue;
    }

    public void setOldValue(Object oldValue) {
        this.oldValue = oldValue;
    }

    public boolean syncToResourceContent() {
        this.resourceContent.setChanged(true);
        this.resourceContent.syncGroupData(this.group, this.getDataVector());
        boolean ret = this.resourceContent.save();
        if (ret) {
            this.resourceContent.setChanged(false);
        }
        return ret;
    }

    /*
     * JTable uses this method to determine the default renderer/
     * editor for each cell. If we didn't implement this method,
     * then the last column would contain text ("true"/"false"),
     * rather than a check box.
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public Class getColumnClass(int c) {
//        return getValueAt(0, c).getClass();
        return this.columnClasses[c];
    }

    /*
     * Don't need to implement this method unless your table's
     * editable.
     */
    public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
        if (this.getColumnCount() == 0)
            return false;
        if (col == this.getColumnCount() - 1)
            return false;
        if (Constants.MEDIA_CATEGORY.equals(this.category)) {
            return col != 1;
        }
        return true;
    }

    public void addRow() {
        Object[] rowData = new Object[this.getColumnCount()];
        for (int i = 0; i < this.getColumnCount() - 1; i++) {
            rowData[i] = "";
        }
        addRow(rowData);
    }

    private void loadImage(Vector<Vector> data) {
        for (Vector<Object> row : data) {
            String imageFilename = (String) row.get(MediaResourceContent.FULL_FILENAME_COLUMN_INDEX);
            ImageIcon icon = SwingUtils.shrinkIcon(imageFilename, Constants.MEDIA_ICON_SIZE);
//            ImageIcon icon = new ImageIcon(Objects.requireNonNull(imageFilename));
//            Image img = icon.getImage().getScaledInstance(Constants.MEDIA_ICON_SIZE, Constants.MEDIA_ICON_SIZE, Image.SCALE_SMOOTH);
            row.set(MediaResourceContent.ICON_COLUMN_INDEX, icon);
        }
    }

    private void printDebugData() {
        if (!Constants.DEBUG)
            return;
        int numRows = getRowCount();
        int numCols = getColumnCount();
        for (int i = 0; i < numRows; i++) {
            LOG.debug("printDebugData", " row " + i + ":");
            for (int j = 0; j < numCols; j++) {
//                System.out.print(" " + data[i][j]);
            }
        }
        LOG.debug("printDebugData", "--------------------------");
    }
}
